﻿using MediatR;
using WeekWork929.Api.Read.Appliaction.Commond;
using WeekWork929.Api.Read.Dto;
using WeekWork929.Domain;
using WeekWork929.ErrorCode;
using WeekWork929.infrastrunceture;

namespace WeekWork929.Api.Read.Appliaction.CommondHandler
{
    public class ShowMeasonHandler : IRequestHandler<ShowMeason, ApiResult<ApiPaging<ShowMeaDto>>>
    {
        private readonly IBaseIRepository<Masenmdooe> masenmdooe;

        public ShowMeasonHandler(IBaseIRepository<Masenmdooe> masenmdooe)
        {
            this.masenmdooe = masenmdooe;
        }

        public Task<ApiResult<ApiPaging<ShowMeaDto>>> Handle(ShowMeason request, CancellationToken cancellationToken)
        {
             ApiResult<ApiPaging<ShowMeaDto>> result = new ApiResult<ApiPaging<ShowMeaDto>>();
             result.Code=ApiEnum.修改成功;
             result.Msg=ApiEnum.查询成功.ToString();
            var list = from a in masenmdooe.GetAll()
                       select new ShowMeaDto()
                       {
                           MasenmdooeId=a.MasenmdooeId,
                           MasenmdooeCode=a.MasenmdooeCode,
                           MasenmdooeName=a.MasenmdooeName,
                           MasenmdooeNode=a.MasenmdooeNode,
                           Number=a.Number, 
                           CreateTime=a.CreateTime,
                           State=a.State,
                           Desc=a.Desc,
                       };
            var totoCount = list.Count();
            var totoPage=(int)Math.Ceiling(totoCount * 1.0/request.Pagesize);
            list=list.OrderBy(x=>x.Number).Skip((request.Pageindex-1)*request.Pagesize).Take(request.Pagesize);
            ApiPaging<ShowMeaDto> paging = new ApiPaging<ShowMeaDto>();
            paging.totoPage = totoPage;
            paging.totoCount = totoCount;
            paging.data=list.ToList();
            result.data = paging;
            return Task.FromResult(result);
        }
    }
}
